package cn.project.lemontree.controller;

import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.Class;
import cn.project.lemontree.bean.Teacher;
import cn.project.lemontree.service.ClassService;
import cn.project.lemontree.service.TeacherService;
import cn.project.lemontree.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/class")
@Api(tags = "儿童班级管理模块")
@CrossOrigin
public class ClassController {
    @Resource
    private ClassService classService;
    @Resource
    private TeacherService teacherService;

    @ApiOperation("分页查询班级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="className",value="班级名",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name = "teacherName",value = "老师姓名",required = false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult<Map<String, Object>> listpage(int page, int size,
                                                      String className, String teacherName){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",classService.listpage(page,size,className,teacherName));
        map.put("count",classService.count(className,teacherName));
        map.put("page",page);
        classService.countChild();
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除班级")
    @ApiOperation("删除班级信息")
    @GetMapping("/remove")
    public CommonResult remove(@RequestParam("classId") int classId) {
        return CommonResult.success(classService.removeById(classId));

    }

    @SystemControllerLog(description = "添加班级")
    @ApiOperation(value = "添加班级信息")
    @PostMapping("/save")
    public CommonResult saveClass(@RequestBody Class aclass){
        aclass.setTeacher(teacherService.findByid(aclass.getTeacher_id()));
        return CommonResult.success(true);
    }

    @SystemControllerLog(description = "添加班级")
    @ApiOperation(value = "添加班级信息2")
    @PostMapping("/add")
    public CommonResult addClass(@RequestBody Class aclass){
        return CommonResult.success(classService.saveClass(aclass));
    }

    @SystemControllerLog(description = "修改班级")
    @ApiOperation(value = "修改班级信息")
    @PostMapping("/update")
    public  CommonResult updateClass(@RequestBody Class aclass){
        return CommonResult.success(classService.updateClass(aclass));
    }
    @SystemControllerLog(description = "生成班级邀请码")
    @ApiOperation(value = "生成班级邀请码")
    @PostMapping("/setInvitationCode")
    public CommonResult setInvitationCode(Integer classId){
        return CommonResult.success(classService.setInvitationCode(classId));
    }

    @GetMapping("/getdetails")
    public CommonResult getDetails(Integer childId){
        return CommonResult.success(classService.getClassdetails(childId));
    }

    @SystemControllerLog(description = "查询空班级")
    @ApiOperation(value = "查询空班级")
    @GetMapping("/findemptyclass")
    public CommonResult findEmptyClass(){
        classService.countChild();
        Map<String ,Object> map = new HashMap<>();
        map.put("data",classService.findEmptyClass());
        return CommonResult.success(map);
    }
}
